package ca.polymtl.liom.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JFormattedTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.text.ParseException;

public class LIOMExpStartDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel p_base_pathname_lbl;
	private JComboBox p_comboBox;
	private JFormattedTextField p_formattedTextField;
	private JTextArea p_txtrEnterExperimentComments;
	private String p_exp_base_pathname=null;
	private String p_exp_number="00";;
	private String p_exp_short_name=null;
	private String p_exp_comments=null;
	private boolean p_data_valid=false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			LIOMExpStartDialog dialog = new LIOMExpStartDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
			if(dialog.dataValid()){
				System.out.println(dialog.getExpBaseFolder());
				System.out.println(dialog.getExpNumber());
				System.out.println(dialog.getExpShortName());
				System.out.println(dialog.getExpComments());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public LIOMExpStartDialog() {
		setTitle("Experiment Initialization");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setToolTipText("Base entry point to any experiment.");
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			JLabel lblExperimentShortName = new JLabel("Experiment short name:");
			lblExperimentShortName.setHorizontalAlignment(SwingConstants.LEFT);
			contentPanel.add(lblExperimentShortName, "2, 2, right, default");
		}
		{
			p_comboBox = new JComboBox();
			p_comboBox.setModel(new DefaultComboBoxModel(new String[] {"Test"}));
			contentPanel.add(p_comboBox, "4, 2, fill, default");
		}
		{
			JButton btnNew = new JButton("New");
			btnNew.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String str = JOptionPane.showInputDialog(null, "Enter short experiment name: ","LIOM", 1);
					if(str != null){
						p_comboBox.addItem(str);
						p_comboBox.setSelectedItem(str);
					}
				}
			});
			contentPanel.add(btnNew, "6, 2");
		}
		{
			JLabel lblExperimentNumber = new JLabel("Experiment number:");
			lblExperimentNumber.setHorizontalAlignment(SwingConstants.LEFT);
			contentPanel.add(lblExperimentNumber, "2, 4, right, default");
		}
		{
			MaskFormatter formatter = null;
			try {
				formatter = new MaskFormatter("##");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			p_formattedTextField = new JFormattedTextField(formatter);
			p_formattedTextField.setToolTipText("Experiment number, must be an integer.");
			p_formattedTextField.setText("00");
			contentPanel.add(p_formattedTextField, "4, 4, fill, default");
		}
		{
			JLabel lblExperimentBaseFolder = new JLabel("Experiment base folder:");
			lblExperimentBaseFolder.setHorizontalAlignment(SwingConstants.LEFT);
			contentPanel.add(lblExperimentBaseFolder, "2, 6, right, default");
		}
		{
			p_base_pathname_lbl = new JLabel();
			p_base_pathname_lbl.setText("/Users/flesage/");
			contentPanel.add(p_base_pathname_lbl, "4, 6, fill, default");
		}
		{
			JButton btnChoose = new JButton("Choose");
			btnChoose.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.setDialogTitle("Choose Base Experiment Folder");
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					if( fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
							p_exp_base_pathname = fc.getSelectedFile().getAbsolutePath()+File.separator;
							p_base_pathname_lbl.setText(p_exp_base_pathname);							
					}
				}
			});
			contentPanel.add(btnChoose, "6, 6");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						p_exp_base_pathname = p_base_pathname_lbl.getText();
						p_exp_short_name = p_comboBox.getSelectedItem().toString();
						p_exp_number = p_formattedTextField.getText();
						p_exp_comments = p_txtrEnterExperimentComments.getText();
						p_data_valid=true;
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			p_txtrEnterExperimentComments = new JTextArea();
			p_txtrEnterExperimentComments.setText("Enter experiment comments here (animal physiology etc\u2026)");
			getContentPane().add(p_txtrEnterExperimentComments, BorderLayout.CENTER);
		}
	}
	public String getExpBaseFolder(){
		return p_exp_base_pathname;
	}
	public String getExpComments(){
		return p_exp_comments;
	}
	public String getExpNumber(){
		return p_exp_number;
	}
	public String getExpShortName(){
		return p_exp_short_name;
	}
	public boolean dataValid(){
		return p_data_valid;
	}
}
